Skip to main content

Activity Selection

The Activity Selection problem is a classic example of a greedy algorithm application. It is concerned with selecting the maximum number of activities that don't overlap in time from a given set of activities.

Problem Statement

Given n activities with their start and finish times, select the maximum number of activities that a single person can attend without any overlap. Each activity is represented by a start time s[i] and a finish time f[i].

Greedy Strategy

The optimal greedy strategy for the activity selection problem involves selecting activities based on their finishing times. Here are the steps:

  1. Sort the activities based on their finish times.
  2. Select the first activity from the sorted list and add it to the result as it finishes the earliest.
  3. Iterate through the remaining activities and select the next activity if its start time is greater than or equal to the finish time of the last selected activity.

Algorithm

  1. Sort all the activities by their finishing time.
  2. Initialize lastSelectedFinishTime to 0 (or negative infinity if using time).
  3. Initialize selectedActivities to an empty list.
  4. For each activity in the sorted list:
    • If the start time of the activity is greater than or equal to lastSelectedFinishTime:
      • Add the activity to selectedActivities.
      • Update lastSelectedFinishTime to the finish time of the current activity.
  5. Return selectedActivities as the list of chosen activities.
Greedy-Activity-Selector(A, s, f):
Sort A by finish times stored in f
S = {A[1]} # Select the first activity
k = 1
for i = 2 to n:
if s[i] >= f[k]:
S = S U {A[i]} # Select the activity
k = i
return S

Complexity

  • Time Complexity: O(nlogn)O (n \log n) due to the sorting step, followed by an O(n)O (n) pass to select activities.
  • Space Complexity: O(1)O (1), not counting the space needed for output; this is due to the inplace sorting and a constant number of extra variables.

Example of the Activity Selection Problem

Suppose we have the following set of activities (labeled from A to E) with their respective start and finish times:

ActivityStart TimeFinish Time
A14
B35
C06
D57
E89

Applying the Greedy Algorithm

  1. Step 1: Sort Activities by Finish Time

    • Sorted order of activities based on their finish times: A, B, D, E, C
  2. Step 2: Select Activities

    • Start with the first activity in the sorted list.
    • Activity A (1, 4)
    • Next, we skip B because it overlaps with A (it starts before A finishes).
    • The next activity that doesn't overlap with A is D (5, 7), so we select it.
    • Finally, activity E (8, 9) does not overlap with D and can be selected.

Selected Activities

  • A (Starts at 1, finishes at 4)
  • D (Starts at 5, finishes at 7)
  • E (Starts at 8, finishes at 9)

These activities are chosen because they allow for the maximum number of non-overlapping activities (three in this case). If you choose any activity like C that spans a long duration (0, 6), it would block out multiple other shorter activities, thus reducing the overall count.

Proof of Optimality

Step 1: Definition of Sets

  • Let AA be the set of activities selected by the greedy algorithm.
  • Let BB be any other set of activities that also do not overlap.

Step 2: Assumptions

  • The greedy algorithm selects activities based on the earliest finishing times.
  • Assume that activities in both AA and BB are sorted by their finish times. Let a1,a2,,aka_1, a_2, \ldots, a_k be the activities in AA and b1,b2,,bmb_1, b_2, \ldots, b_m be the activities in BB, where f(ai)f (a_i) and f(bi)f (b_i) denote their respective finish times.

Step 3: Method of Substitution

  • The goal is to show that the number of activities in AA (|A|) is at least as large as the number of activities in BB (|B|), i.e., AB|A| \geq |B|.

Step 4: Proof by Induction

  1. Base Case: Compare a1a_1 and b1b_1. Since a1a_1 is selected by the greedy algorithm, it has the earliest finish time among all activities and thus f(a1)f(b1)f (a_1) \leq f (b_1).
  2. Inductive Step:
    • Assume that for the first jj activities in BB, they can be replaced by activities in AA such that f(ai)f(bi)f (a_i) \leq f (b_i) for all iji \leq j.
    • Consider the next activity bj+1b_{j+1}. Since bj+1b_{j+1} does not overlap with bjb_j, we have s(bj+1)f(bj)s (b_{j+1}) \geq f (b_j).
    • From the greedy selection, aj+1a_{j+1} is the next activity selected after aja_j with f(aj)f(bj)f (a_j) \leq f (b_j), and thus s(aj+1)f(aj)s (a_{j+1}) \geq f (a_j). Therefore, f(aj+1)f (a_{j+1}) must also be less than or equal to f(bj+1)f (b_{j+1}) because aj+1a_{j+1} is the earliest finishing activity available after aja_j.

Step 5: Conclusion

  • By induction, each activity in BB can be matched with an activity in AA that finishes no later than itself, which implies AB|A| \geq |B|.
  • Since BB was any arbitrary set of non-overlapping activities, AA must be the largest possible set of non-overlapping activities.
  • Therefore, the set AA selected by the greedy algorithm is optimal.